package edu.neusoft.yuejian_09.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.Question;
import edu.neusoft.yuejian_09.model.MasterModel;

public class MasterAskActivity extends Activity implements RetrofitListener<Question> {
    private ImageView img_return;
    private EditText noteContent;
    private Button Submit;

    private int user_id;
    private int master_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_master_ask);
        initViews();
        initEvents();
        getUserId();
    }

    public void initViews(){

        img_return=(ImageView)findViewById(R.id.img_return);
        noteContent=(EditText)findViewById(R.id.add_note);
        Submit=(Button) findViewById(R.id.insure);

    }

    public void initEvents(){
        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pcontent=noteContent.getText().toString();
                if (pcontent.equals("")){
                    Toast.makeText(MasterAskActivity.this,"请输入发帖信息", Toast.LENGTH_SHORT).show();
                }else {
                    submitnote();
                }
            }
        });

        img_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
    }

    //获取个人用户信息
    public void getUserId(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
    }

    //提交内容
    private void submitnote() {
        Bundle bundle = getIntent().getExtras();
        master_id = bundle.getInt("master_id");
        String qa_question=noteContent.getText().toString();
        MasterModel masterModel = new MasterModel();
        masterModel.insertNote(user_id,master_id,qa_question,this);
    }

    @Override
    public void onSuccess(Question data,int flag ){
        try{
            Toast.makeText(MasterAskActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
            finish();

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void onFail(){ Toast.makeText(MasterAskActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}

}
